﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace LabMS.Web.Equipment.Valuables
{
    public partial class addvaluables : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("GZSBGL");//必须写在ispostback外
            strPageUserType.Add("mana");

            if (!IsPostBack)
            {   
                tbPurchaseDate.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
                tbProductionDate.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
                
                tbKindNum.Attributes.Add("readOnly", "-1");
                tbCountryCode.Attributes.Add("readOnly", "-1");
                tbInstrumentName.Attributes.Add("readOnly", "-1");
                
                tbAmount.Attributes.Add("readOnly", "-1");
                tbCreatedDate.Attributes.Add("readOnly", "-1");
                tbMeasurement.Attributes.Add("readOnly", "-1");
                
                Bind();
            }
        }

        public void Bind()
        {
            tbCreatedDate.Text = DateTime.Today.ToShortDateString();
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();

            listCClass = eDictionary.GetItemListByDClass("shebeilaiyuan");
            ddlSource.DataSource = listCClass;
            ddlSource.DataTextField = "Name";
            ddlSource.DataValueField = "Code";
            ddlSource.DataBind();

            listCClass = eDictionary.GetItemListByDClass("shiyongfangxiang");
            ddlArrow.DataSource = listCClass;
            ddlArrow.DataTextField = "Name";
            ddlArrow.DataValueField = "Code";
            ddlArrow.DataBind();

            listCClass = eDictionary.GetItemListByDClass("shebeizhuangtai");
            ddlPresentSituation.DataSource = listCClass;
            ddlPresentSituation.DataTextField = "Name";
            ddlPresentSituation.DataValueField = "Code";
            ddlPresentSituation.DataBind();

            listCClass = eDictionary.GetItemListByDClass("jingfeikemu");
            ddlFundsSubject.DataSource = listCClass;
            ddlFundsSubject.DataTextField = "Name";
            ddlFundsSubject.DataValueField = "Code";
            ddlFundsSubject.DataBind();
        }

        protected void lbSave_Click(object sender, EventArgs e)
        {
            Model.InstrumentAndConsumables valuables = new LabMS.Model.InstrumentAndConsumables();
            BLL.InstrumentAndConsumables bvaluables = new LabMS.BLL.InstrumentAndConsumables();

            valuables.Numbers = tbInstrumentNum.Text.Trim();
            valuables.ClassNumber = tbKindNum.Text.Trim();
            valuables.Name = tbInstrumentName.Text.Trim();
            valuables.Standards = tbStandards.Text.Trim();
            valuables.Model = tbModel.Text.Trim();
            valuables.CountryCode = tbCountryCode.Text.Trim();
            valuables.StockLocation = tbLocationCode.Text;
            valuables.SpaceLocation = tbLocationCode.Text;
            valuables.Quantity = Convert.ToInt32(tbAmount.Text);
            valuables.UnitPrice = tbUnitPrice.Text.Trim();
            valuables.FactoryCode = tbFactoryCode.Text.Trim();
            valuables.Documents = tbDocuments.Text.Trim();
            valuables.Measurement = tbMeasurement.Text.Trim();
            valuables.Source = ddlSource.SelectedValue;

            DateTime tempTime = new DateTime();
            if (DateTime.TryParse(tbProductionDate.Text.Trim(), out tempTime))
            {
                valuables.ProductionDate = tempTime;
            }
            if (DateTime.TryParse(tbPurchaseDate.Text.Trim(), out tempTime))
            {
                valuables.PurchaseDate = tempTime;
            }
            if (DateTime.TryParse(tbOpeningTime.Text.Trim(), out tempTime))
            {
                valuables.OpeningTime = tempTime;
            }

            valuables.CreatedDate = Convert.ToDateTime(tbCreatedDate.Text);
            valuables.Arrow = ddlArrow.SelectedValue;
            valuables.PresentSituation = ddlPresentSituation.SelectedValue;
            valuables.FundsSubject = ddlFundsSubject.SelectedValue;
            valuables.ResponesoblePerson = tbResponesoblePerson.Text.Trim();
            valuables.ContactPhone = tbContactPhone.Text.Trim();
            valuables.Charges = tbCharges.Text.Trim();
            valuables.ZipCode = tbZipCode.Text.Trim();
            valuables.MailingAddress = tbMailingAddress.Text.Trim();
            valuables.Purpose = tbPurpose.Text.Trim();
            valuables.OperatorDate = DateTime.Now;
            valuables.Type = "guizhongyiqi";
            //上传图片
            try
            {
                if (imageUpload.HasFile)
                {
                    HttpPostedFile upPhoto = imageUpload.PostedFile;
                    int upPhotoLength = upPhoto.ContentLength;
                    string PhotoContentType = upPhoto.ContentType;
                    byte[] PhotoArray = new byte[upPhotoLength];
                    Stream PhotoStream = upPhoto.InputStream;
                    PhotoStream.Read(PhotoArray, 0, upPhotoLength);

                    valuables.Pic = PhotoArray;//相片
                    valuables.PicType = PhotoContentType;//图片类型
                }
            }
            catch
            {
                LabMS.Common.JShelper.JSAlert(Page, "Picwrong", "程序错误:图片保存出错");
            }
            try
            {
                bvaluables.Add(valuables);
                LabMS.Common.JShelper.JSAlertAndRedirect(Page, "AddSuccess", "添加成功!", "addvaluables.aspx");
            }
            catch (Exception ex)
            {
                Err.Text = ex.Message.ToString();
            }


        }
    }
}
